<?php
#!/usr/bin/php

$list = array('apple'=>300, 'zoo'=>120, 'orange'=>150, 'banana'=>100, 'mango'=>330);

print_r(selection_sort($list, 'key', True));
//print_r(selection_sort($list, 'value'));

function compare($a, $b){
	if (is_numeric($a)) {
		return $a > $b;
	} else {
		return strcmp($a, $b) > 0;
	}
}

function selection_sort($array, $orderby = 'value', $asc = True){
	if (empty($array)) {
		return array();
	}

	if ($orderby == 'key') { // keys are unique
		$array_keys = array_keys($array);
		$array_keys = selection_sort($array_keys,'',$asc);

		foreach ($array_keys as $key) {
			echo "key:" . $key . " , array:" . $array[$key] . "<br />";
			$result[$key] = $array[$key];
		}

		return $result;
	}

	$result = array();

	foreach ($array as $v) {
		$min = NULL;
		$min_key;
		$min_value;

		foreach ($array as $key => $value) {
			if ($min == NULL || compare($min, $value) == $asc) {
				$min = $value;
				$min_key = $key;
				$min_value = $value;
			}
		}

		if (!is_int($min_key)) {
			$result[$min_key] = $min_value;
		} else {
			$result[] = $min_value;
		}
		unset($array[$min_key]);
	}

	return $result;
}

$myStr = "The cow jumped over the moon";
$myArray = explode(" ", $myStr);
$myCount =count($myArray)-1;
for ($i=$myCount;$i>=0;$i--) {
	echo $myArray[$i] . " ";
}
echo "<br />Strcmp: ";
echo strcmp ("a","a");
echo "<br />";

$list2 = array("55","88","10","13","43","25");
echo "bubble: " . bubbleSort($list2);

 function bubbleSort ($items) {
     $size = count($items);
     for ($i=0; $i<$size; $i++) {
          for ($j=0; $j<$size-1-$i; $j++) {
               if ($items[$j+1] < $items[$j]) {
                   arraySwap($items, $j, $j+1);
               }
          }
     }
     for ($i=0; $i<$size; $i++) {
     	echo $items[$i] . " ";
     }
     return $items;
 }
 function arraySwap (&$arr, $index1, $index2) {
     $tmp = $arr[$index1];
     $arr[$index1] = $arr[$index2];
     $arr[$index2] = $tmp;
 }
echo "<br />=====================================================<br />";

function mySortKey ($myArray) {
	$myCount = count($myArray);
	$myMaxKey = NULL;
	$myMaxValue;
	for ($i=0;$i<$myCount;$i++) {
		foreach ($myArray as $thisKey => $thisValue) {
			if ($myMaxKey != NULL ) {
				if (strcmp($myMaxKey,$thisKey) >0 ) {
					$newArray[$thisKey]=$thisValue;
				} else {
					$newArray[$myMaxKey]=$myMaxValue;
					$myMaxKey=$thisKey;
					$myMaxValue=$thisValue;
				}
			} else {
				$myMaxKey=$thisKey;
				$myMaxValue=$thisValue;
			}
		}
		$newArray[$myMaxKey]=$myMaxValue;
		$myMaxKey = NULL;
		$myArray = $newArray;
		$newArray = NULL;
	}

    //show result
	foreach ($myArray as $thisKey => $thisValue) {
		echo "[" . $thisKey . "]" . $thisValue . ",";
	}

} 
mySortKey($list);

echo "<br />=====================================================<br />";
function mySortValue ($myArray) {
	$myCount = count($myArray);
	$myMaxKey = NULL;
	$myMaxValue = NULL;

	
	for ($i=0;$i<$myCount;$i++) {
		foreach ($myArray as $thisKey => $thisValue) {
			if ($myMaxValue != NULL ) {
				if ($myMaxValue > $thisValue ) {
					$newArray[$thisKey]=$thisValue;
				} else {
					$newArray[$myMaxKey]=$myMaxValue;
					$myMaxKey=$thisKey;
					$myMaxValue=$thisValue;
				}
			} else {
				$myMaxKey=$thisKey;
				$myMaxValue=$thisValue;
			}
		}
		$newArray[$myMaxKey]=$myMaxValue;
		$myMaxValue = NULL;
		$myArray = $newArray;
		$newArray = NULL;
	}	
	
	
    //show result
	foreach ($myArray as $thisKey => $thisValue) {
		echo "[" . $thisKey . "]" . $thisValue . ",";
	}
}
mySortValue($list);

?>

